//
//  SubjectViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-8.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SubjectViewController.h"
#import "AppModel.h"
#import "SubjectCell.h"
#import "SubjectModel.h"
#import "DetailViewController.h"
#import "JHRefresh.h"


@interface SubjectViewController ()

@end

@implementation SubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //把父类不适合子类的操作 修改
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    
    //不需要 搜索
    self.tableView.tableHeaderView = nil;
    //无分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 304;
    
    //注册 Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:@"SubjectCell"];
    
    [self firstDownload];
    [self creatRefreshView];
}
#pragma mark - 重写父类的方法
//专题界面要重写父类的方法
- (void)firstDownload {
    self.currentPage = 1;
    self.isRefreshing = NO;
    self.isLoadMoring = NO;
    NSString *url = [NSString stringWithFormat:kSubjectUrl,self.currentPage];
    [self addTaskUrl:url isRefresh:YES];
}

- (void)addTaskUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
    
    __weak typeof(self) weakSelf = self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            //最外层是数组
            NSArray * arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in arr) {
                SubjectModel *subjectModel = [[SubjectModel alloc] init];
                [subjectModel setValuesForKeysWithDictionary:dict];
                //我们要把 applications数组中对应数据换成AppModel
                NSMutableArray *appArr = [[NSMutableArray alloc] init];
                for (NSDictionary *appDict in dict[@"applications"]) {
                    AppModel *appModel = [[AppModel alloc] init];
                    [appModel setValuesForKeysWithDictionary:appDict];
                    [appArr addObject:appModel];
                }
                //在重新放入subjectModel
                subjectModel.applications = appArr;
                
                //放入数据源数组
                [weakSelf.dataArr addObject:subjectModel];
            }
            //刷新表格
            [weakSelf.tableView reloadData];
            [weakSelf endRefreshing];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"专题下载失败");
    }];
}

- (void)creatRefreshView {
    __weak typeof(self) weakSelf = self;
    
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.currentPage = 1;
        weakSelf.isRefreshing = YES;
        NSString *url = [NSString stringWithFormat:kSubjectUrl,weakSelf.currentPage];
        
        [weakSelf addTaskUrl:url isRefresh:YES];
    }];
    
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        //上拉加载更多
        if (weakSelf.isLoadMoring) {
            return ;
        }
        weakSelf.isLoadMoring = YES;
        weakSelf.currentPage++;
        NSString *url = [NSString stringWithFormat:kSubjectUrl,weakSelf.currentPage];
        [weakSelf addTaskUrl:url isRefresh:YES];
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubjectCell" forIndexPath:indexPath];
    SubjectModel *model = self.dataArr[indexPath.row];
    //填充cell
    __weak typeof (self)weakSelf = self;
    [cell showDataWithModel:model withBlock:^(AppModel *model) {
        //实现界面跳转
        DetailViewController *detail = [[DetailViewController alloc] init];
        detail.applicationId = model.applicationId;
        detail.category = kSubjectType;
        [weakSelf.navigationController pushViewController:detail animated:YES];
    }];
    return cell;
}
//重写父类的方法 专题界面不能选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
